<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 移动文件到指定文件夹
 * @author xuelange
 *
 */
class F_move extends MY_Controller{
	public function __construct(){
		parent::__construct();
		$this->load->model('files_model');
		$this->load->model('folder_mdl');
	}
	
	/**
	 * 获取指定pid下的文件夹
	 */
	public function index(){
		$uuid = $this->input->get("node");
		$newList = array();
		if(strlen($uuid) == 0){
			$uuid = "0";
		}
		
		$pid = 0;
		if($uuid != "0"){
			$folder = $this->folder_mdl->get_folder_by_uuid($uuid);
			$pid = $folder['id'];
		}
		
		$folders = $this->folder_mdl->get_folders_by_pid($pid,$this->uid);
		if(!$folders){
			
			if($pid == 0){
				$arr = array();
				$arr['id'] = "0";
				$arr['label'] = "所有文件";
					
				echo json_encode(array($arr));
				return;
			}
			common::prf_json(FALSE,NULL,NULL,time(),TRUE,"folders is null");
			return;
		}
	
		
		foreach ($folders as $key => $folder){
			$newList[$key]['label'] = $folder['name'];
			$newList[$key]['id'] = $folder['uuid'];
			$newList[$key]['load_on_demand'] = ($folder['has_children'] == 1) ? true : false;
		}
		if($uuid == "0"){
			$arr = array();
			$arr['id'] = "0";
			$arr['label'] = "所有文件";
			$arr['children'] = $newList;
			
			echo json_encode(array($arr));
			return;
		}
		echo json_encode($newList);
		return;
		
	}
	
	/**
	 * 更新文件的pid
	 */
	public function update(){
		$file_uuids = $this->input->post("fileIds",TRUE);
		if(!is_array($file_uuids)){
			common::prf_json(FALSE,NULL,NULL,time(),TRUE,"fileIds is not array");
			return;
		}
		
		$uuid = $this->input->post("folder_id",TRUE);
		if(strlen($uuid) == 0){
			$uuid = "0";
		}
		
		$pid = 0;
		if($uuid != "0"){
			$folder = $this->folder_mdl->get_folder_by_uuid($uuid);
			$pid = $folder['id'];
		}
		
		$this->db->select("pid");
		$file = $this->db->get_where("file",array(
			"uuid" => $file_uuids[0]
		))->row_array();
		
		if(!$file){
			common::prf_json(FALSE,NULL,NULL,time(),TRUE,"file is not exsit");
			return;
		}
		
		if($file['pid'] == $pid){
			common::prf_json(FALSE,NULL,NULL,time(),TRUE,"dir is not change");
			return;
		}
		
		$this->db->trans_strict(TRUE);
		$this->db->trans_begin();
		
		//批量移动文件到指定文件夹
		$flag = $this->files_model->update_pid_batch($file_uuids,$pid);
		
		if(!$flag){
			common::prf_json(FALSE,NULL,NULL,time(),TRUE,"updata is fail");
			$this->db->trans_rollback();
			return;
		}
		
		
		//获取父文件夹的信息
		if($pid == 0){
			$parent_folder = array();
			$parent_folder['access_denied'] = 0;
		}else{
			$parent_folder = $this->folder_mdl->get_parent_by_id($pid);
		}
		
		if(! $parent_folder){
			common::prf_json(FALSE,NULL,NULL,time(),TRUE,"parent_folder is null");
			$this->db->trans_rollback();
			return;
		}
		
		//获取需要设置的uuids
		$this->db->select("uuid");
		$this->db->where(array(
			"pid" => $pid,
			"access_denied" => 0,
			"deleted" => 0
		));
		$this->db->where_in("uuid",$file_uuids);
		$need_uuids = $this->db->get("file")->result_array();
		if(!$need_uuids){
			$this->trans_commit();
			common::prf_json(TRUE,NULL);
			return;
		}
		
		
		//根目录不用添加策略
		if($pid == 0){
			$this->trans_commit();
			common::prf_json(TRUE,NULL);
			return;
		}
		
		//获取已有策略的uuid集合
		$exist_uuids = $this->get_exist_uuids($file_uuids);
		
		//获取需要添加策略的uuid集合
		$need_add_uuids = $this->get_need_add_uuids($file_uuids, $exist_uuids);
		
		if(! $need_add_uuids){
			$this->trans_commit();
			common::prf_json(TRUE,NULL);
			return;
		}
		
		//获取需要添加策略的uuid,hash_uuid集合
		$this->db->select("uuid,hash_uuid");
		$this->db->where_in("uuid",$need_add_uuids);
		$files = $this->db->get("file")->result_array();
		
		if(! $files){
			common::prf_json(FALSE,NULL,NULL,time(),TRUE,"files is null");
			$this->db->trans_rollback();
			return;
		}
		
		//添加策略
		$this->add_policies($parent_folder['uuid'], $files);
		
		$this->trans_commit();
		
		common::prf_json(TRUE,NULL);
		return;
	}
	
	/**
	 * 添加策略
	 * @param string parent_uuid 
	 * @param array $uuids
	 */
	public function add_policies($parent_uuid,$files){
		//查询父文件夹的策略
		$this->db->select("pid,value_set,value");
		$file_policies = $this->db->get_where("file_policy",array(
				"file_uuid" => $parent_uuid
		))->result_array();
		
		if(! $file_policies){
			return true;
		}
		
		$newList = array();
		$j = 0;
		foreach ($files as $file){
			foreach ($file_policies as  $file_policy){
				$newList[$j]["file_uuid"] = $file['uuid'];
				$newList[$j]["hash_file_uuid"] = $file['hash_uuid'];
				$newList[$j]["pid"] = $file_policy['pid'];
				$newList[$j]["value_set"] = $file_policy['value_set'];
				$newList[$j]["value"] = $file_policy['value'];
				$newList[$j]["creation_ts"] = time()*1000;
				$j++;
			}
		}
		
		$this->load->model('policies_mdl');
		$this->policies_mdl->addBatch($newList);
		
		return true;
	}
	
	
	/**
	 * 获取已有策略的uuids
	 * @param array uuids
	 */
	public function get_exist_uuids($uuids){
		$this->db->select("file_uuid");
		$this->db->distinct();
		$this->db->where_in("file_uuid",$uuids);
		$list = $this->db->get("file_policy")->result_array();
		
		return $list;
	}
	
	/**
	 * 获取需要添加策略的uuids
	 * @param array $uuids
	 * @param array $exist_uuids
	 */
	public function get_need_add_uuids($uuids,$exist_uuids){
		$newList = array();
		foreach ($uuids as $uuid){
			$flag = false;
			foreach ($exist_uuids as $obj){
				if($uuid == $obj['file_uuid']){
					$flag = true;
					continue;
				}
			}
			if(! $flag){
				$newList[] = $uuid;
			}
		}
		
		return $newList;
	}	
	
}
